
#define ZGL_IMPORT
#include "StrawGUI.h"
#include "zglEngine.h"

using namespace sgui;

static sGUI gui(new sgui::engine::zglEngine());

void Draw()
{
	gui.draw();
}

void Init()
{
	sLibrary &lib = *gui.getLibrarys().add(new sgui::sLibrary(gui, L"main"));

    res::sTextureResource* texResource = new res::sTextureResource(gui, L"texture1");

    texResource->addContent()->loadFromFile(L"edit.png");
    lib.getResources().add(texResource);

    preset::sTexturePreset* texPreset = new preset::sTexturePreset(gui, L"texturePreset1", *texResource);
	texPreset->init(gui.getEngine());

    lib.getPresets().add(texPreset);

    res::sFontResource* fntResource = new res::sFontResource(gui, L"font1");

	fntResource->addContent()->loadFromFile(L"font0.zfi");
	fntResource->addContent()->loadFromFile(L"font0-page0.png");
    lib.getResources().add(fntResource);

    preset::sFontPreset* fntPreset_normal = new preset::sFontPreset(gui, L"fontPreset1", *fntResource);
    fntPreset_normal->setColor(0xFFFF33FF);
    lib.getPresets().add(fntPreset_normal);
    preset::sFontPreset* fntPreset_active = new preset::sFontPreset(gui, L"fontPreset1", *fntResource);
    fntPreset_active->setColor(0xFF33FFFF);
    lib.getPresets().add(fntPreset_active);

    res::sTextureResourceModule* moduleNormal = new res::sTextureResourceModule(gui, L"mNormal");
    res::sTextureResourceModule* moduleHover = new res::sTextureResourceModule(gui, L"mHover");
    res::sTextureResourceModule* moduleActive = new res::sTextureResourceModule(gui, L"mActive");

    moduleNormal->getPreset().set(texPreset);
    moduleHover->getPreset().set(texPreset);
    moduleActive->getPreset().set(texPreset);

    moduleNormal->getRect().set(0, 0, 32, 32);
    moduleHover->getRect().set(32, 0, 32, 32);
    moduleActive->getRect().set(0, 32, 32, 32);

    texResource->getModules().add(moduleNormal);
    texResource->getModules().add(moduleHover);
    texResource->getModules().add(moduleActive);

    res::sTextureResourceFrame *frameNormal = new res::sTextureResourceFrame(gui, L"fNormal");
    res::sTextureResourceFrame *frameHover = new res::sTextureResourceFrame(gui, L"fHover");
    res::sTextureResourceFrame *frameActive = new res::sTextureResourceFrame(gui, L"fActive");

    frameNormal->getObjects().push_back(new res::sTextureResourceFrame::sFrameObject(gui, moduleNormal));
    frameHover->getObjects().push_back(new res::sTextureResourceFrame::sFrameObject(gui, moduleHover));
    frameActive->getObjects().push_back(new res::sTextureResourceFrame::sFrameObject(gui, moduleActive));

    texResource->getFrames().add(frameNormal);
    texResource->getFrames().add(frameHover);
    texResource->getFrames().add(frameActive);

    frameNormal->setSize(math::sPointf(32, 32));
    frameHover->setSize(math::sPointf(32, 32));
    frameActive->setSize(math::sPointf(32, 32));
    
    frameNormal->getStretch().setMode(res::ESM_STRETCH);
    frameHover->getStretch().setMode(res::ESM_STRETCH);
    frameActive->getStretch().setMode(res::ESM_STRETCH);

    frameNormal->getStretch().getDivider(res::ESD_FIRST).set(0.45f, 0.45f);
    frameNormal->getStretch().getDivider(res::ESD_SECOND).set(0.45f, 0.45f);
    frameHover->getStretch().getDivider(res::ESD_FIRST).set(0.45f, 0.45f);
    frameHover->getStretch().getDivider(res::ESD_SECOND).set(0.45f, 0.45f);
    frameActive->getStretch().getDivider(res::ESD_FIRST).set(0.45f, 0.45f);
    frameActive->getStretch().getDivider(res::ESD_SECOND).set(0.45f, 0.45f);

    frameNormal->update();
    frameHover->update();
    frameActive->update();

    res::sComponentEdit *cmpEdit = new res::sComponentEdit(gui, L"cmpEdit");
    cmpEdit->getRect().set(10, 10, 100, 40);

    res::sComponentRectFrame *crFrame = new res::sComponentRectFrame(gui);
    crFrame->getRect().set(0, 0, 100, 40);
    crFrame->getFrame().getNormal().set(frameNormal);
    crFrame->getFrame().getHover().set(frameHover);
    crFrame->getFrame().getActive().set(frameActive);
	crFrame->getAlign().setAttachPoint(res::sComponentAlign::EAP_RIGHT, true);
	crFrame->getAlign().setAttachPoint(res::sComponentAlign::EAP_BOTTOM, true);
    cmpEdit->getComponentRects().push_back(crFrame);

    res::sComponentRectEditableText *crText = new res::sComponentRectEditableText(gui);
    crText->getRect().set(0, 0, 100, 40);
    crText->getFontPresetGroup().getNormal().set(fntPreset_normal);
    crText->getFontPresetGroup().getHover().set(fntPreset_active);
    crText->getFontPresetGroup().getActive().set(fntPreset_active);
	crText->getAlign().setAttachPoint(res::sComponentAlign::EAP_RIGHT, true);
	crText->getAlign().setAttachPoint(res::sComponentAlign::EAP_BOTTOM, true);

    cmpEdit->getComponentRects().push_back(crText);

    cmpEdit->fillRequredRects();
    cmpEdit->setRequiredRect(res::sComponentEdit::REQUIRED_RECT_TEXT, crText);

    res::sObjectLabel *objEdit = new res::sObjectLabel(gui, L"objEdit");
    objEdit->getComponent().set(cmpEdit);
    objEdit->getRect().set(10, 10, 150, 32);
    objEdit->setText(L"Hello, bro! Hoes it goind?");

    static inst::sComponentInstance *instEdit = objEdit->getInstance(gui);
    gui.addInstance(instEdit);

	instEdit = objEdit->getInstance(gui);
	instEdit->setPosition(math::sPointf(200, 10));
    gui.addInstance(instEdit);
}

void Update( double dt )
{
	gui.update(dt);
}

int main()
{   

	zgl_Reg( SYS_LOAD, (void*)&Init );
	zgl_Reg( SYS_DRAW, (void*)&Draw );
	zgl_Reg( SYS_UPDATE, (void*)&Update );

	wnd_SetCaption( "Demo" );
	wnd_ShowCursor( TRUE );
	scr_SetOptions( 800, 550, REFRESH_MAXIMUM, FALSE, FALSE );

    gui.getEngine().init();

	zgl_Init();

    return 0;
}